<?php
class WebApplicationEndBehavior extends CBehavior {

	/**
	 * Web application end's name
	 */
	private $_endName;

	/**
	 * Getter
	 * Allows to get the current -end's name
	 * This way: Yii::app()->endName
	 * @return $_endName
	 */
	public function getEndName() {

		return $this->_endName;
	}

	/**
	 * Run application's end
	 */
	public function runEnd($name) {
		$this->_endName = $name;

		//Attach the changeModulePaths event handler and raise it
		$this->onModuleCreate = array($this, 'changeModulePaths');
		$this->onModuleCreate(new CEvent($this->owner));

		//Run application
		$this->owner->run();
	}

	/**
	 * This event should be raised when CWebApplication
	 * or CWebModule instance are being initialized
	 */
	public function onModuleCreate($event) {
		$this->raiseEvent("onModuleCreate", $event);
	}

	/**
	 * onModuleCreate event handler
	 * A sender must have controllerPath and viewPath properties
	 * @param $event
	 */
	protected function changeModulePaths($event) {
		$event->sender->controllerPath .= DIRECTORY_SEPARATOR.$this->_endName;
		$event->sender->viewPath .= DIRECTORY_SEPARATOR.$this->_endName;
	}
}